package com.example.eventreg.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.eventreg.Classes.AidState;
import com.example.eventreg.Classes.AidVState;
import com.example.eventreg.Student.StudentMapper;
import com.example.eventreg.organizer.Organizer;
import com.example.eventreg.security.JwtUtil;
import com.example.eventreg.sgin.Sgin;
import com.example.eventreg.sgin.SginMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@CrossOrigin("http://localhost:5173/**")
@RequestMapping("/action")
@RestController // 用于定义该类为一个RESTful API控制器
public class ActionController {
    @Autowired
    private ActionMapper actionMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SginMapper sginMapper;
    @Autowired
    private JwtUtil jwtUtil;

    private ActionController() {
        // 初始化一些用户数据
    }
    //获取全部活动
    @GetMapping("/get")
    public List<Action> getAllActions() {
        return actionMapper.selectList(null); // 返回200状态码和用户列表
    }
    //获取一个活动
    //获取单个学生
    @GetMapping("/get/{aid}")
    public List<Action> getOneAction(@PathVariable Long aid) {
        return actionMapper.selectList(new QueryWrapper<Action>().eq("aid",String.valueOf(aid))); // 返回200状态码和用户列表
    }
    //获取单个组织者
    @GetMapping("/getOrg")
    public List<Action> getOneActionOrg(@RequestHeader("Authorization") String token) {
        // 去除 "Bearer " 前缀，获取纯净的 token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        String oid = jwtUtil.getUserIdFromToken(token);
        System.out.println(oid);
        return actionMapper.selectList(new QueryWrapper<Action>().eq("oid",String.valueOf(oid))); // 返回200状态码和用户列表
    }

    @GetMapping("/search/{name}")
    public List<Action> searchAction(@PathVariable String name) {
        name = "%" + name + "%";
        return actionMapper.selectList(new QueryWrapper<Action>().like("aname",name).or().like("des",name)); // 返回200状态码和用户列表
    }
    @GetMapping("/searchName/{name}")
    public List<Action> searchActionName(@PathVariable String name) {
        name = "%" + name + "%";
        return actionMapper.selectList(new QueryWrapper<Action>().like("aname",name)); // 返回200状态码和用户列表
    }
    //根据时间分类
    //获取活动****报名未截止活动（含报名未开始活动）
    @GetMapping("/get/canReg")
    public List<Action> getActionCanReg() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().gt("reg_end",now)); // 返回200状态码和用户列表
    }
    //获取活动****可报名（正在报名）
    @GetMapping("/get/inReg")
    public List<Action> getActionInReg() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().gt("reg_end",now).lt("reg_start",now)); // 返回200状态码和用户列表
    }
    //获取活动****正在活动(可签到时间段)
    @GetMapping("/get/inSgin")
    public List<Action> getActionInSgin() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().gt("act_end",now).lt("act_start",now)); // 返回200状态码和用户列表
    }
    //获取活动****结束前所有活动
    @GetMapping("/get/canSgin")
    public List<Action> getActionCanSgin() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().gt("act_end",now)); // 返回200状态码和用户列表
    }
    //获取活动****已经结束的活动
    @GetMapping("/get/done")
    public List<Action> getActionDone() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().lt("act_end",now)); // 返回200状态码和用户列表
    }

    //获取统计数据
    @GetMapping("/getCount")
    public List<Map<String, Object>> getActionStateCount() {
        return actionMapper.getActionCountByState();
    }



    //学生相关
    //学生参与的所有活动
    @GetMapping("/getS/all")
    public List<Action> getActionAllS(@RequestHeader("Authorization") String token) {
        if (token != null) {
            // 去除 "Bearer " 前缀，获取纯净的 token
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            String sid = jwtUtil.getUserIdFromToken(token);
            List<Sgin> sgins=sginMapper.selectList(new QueryWrapper<Sgin>().eq("sid", sid));
            List<Action> actions = new ArrayList<>();
            for (Sgin sgin : sgins) {
                actions.add(
                        actionMapper.selectOne(new QueryWrapper<Action>().eq("aid", sgin.getAid()))
                );
            }
            return actions;
        }
        return null;
    }

    //根据状态分类
    //未审核
    @GetMapping("/verify/can")
    public List<Action> getActionCanVerify() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().eq("state",0)); // 返回200状态码和用户列表
    }
    //已审核
    @GetMapping("/verify/verified")
    public List<Action> getActionVerified() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().ne("state",0)); // 返回200状态码和用户列表
    }
    //过审
    @GetMapping("/verify/pass")
    public List<Action> getActionPassVerify() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().eq("state",1)); // 返回200状态码和用户列表
    }
    //不过审
    @GetMapping("/verify/not")
    public List<Action> getActionNotVerify() {
        LocalDateTime now = LocalDateTime.now();
        return actionMapper.selectList(new QueryWrapper<Action>().eq("state",2)); // 返回200状态码和用户列表
    }

    //审核状态改变
    // 改变审核状态
    @PutMapping("/verifyState/Change")
    public Action ActionVerifyChange(@RequestBody AidVState obj) {
        Long aid = obj.getAid();;
        Long state = obj.getState();
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(state);
        actionMapper.update(action, updateWrapper);
        return action;
    }
    //待审核
    @PostMapping("/verifyState/wait")
    public Action ActionVerifyWait(@RequestBody Long aid) {
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(0L);
        actionMapper.update(action, updateWrapper);
        return action;
    }
    //审核中
    @PostMapping("/verifyState/ing")
    public Action ActionVerifyIng(@RequestBody Long aid) {
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(1L);
        actionMapper.update(action, updateWrapper);
        return action;
    }//过审
    @PostMapping("/verifyState/pass")
    public Action ActionVerifyPass(@RequestBody Long aid) {
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(2L);
        actionMapper.update(action, updateWrapper);
        return action;
    }
    //不过审
    @PostMapping("/verifyState/not")
    public Action ActionVerifyNot(@RequestBody Long aid) {
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(3L);
        actionMapper.update(action, updateWrapper);
        return action;
    }
    @PostMapping("/verifyState")
    public Action ActionVerifyState(@RequestBody AidState obj) {
        Long aid = obj.getAid();
        int state = obj.getState();
        UpdateWrapper<Action> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("aid",String.valueOf(aid));
        Action action = new Action();
        action.setState(Long.valueOf(state));
        actionMapper.update(action, updateWrapper);
        return action;
    }
    //创建活动
    @PostMapping("/create")
    public Action createAction(@RequestHeader("Authorization") String token ,@RequestBody Action action) {
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        action.setAid(0L);
        String oid = jwtUtil.getUserIdFromToken(token);
        System.out.println(action);
        action.setOid(Long.valueOf(oid));
        actionMapper.insert(action);
        return action;
    }
    //更新
    @PutMapping("/update")
    public int updateAction(@RequestBody Action action) {
        return actionMapper.updateById(action);
    }
    @DeleteMapping("/delete/{aid}")
    public int deleteAction(@PathVariable Long aid) {
        return actionMapper.delete(new QueryWrapper<Action>().eq("aid", aid));
    }
}
